package yudao.btc.eos;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

//模拟浏览器发送http请求完成固定地址的请求
public class EosUtils {

    public static List<EosModel> getEosBlock(String account, Integer maxSeq, JSONObject json) {
        List<EosModel> eosModelList = new ArrayList<EosModel>();
        String url = String.format("https://unicove.com/vaulta/api/account/%s/activity/-%s", account, maxSeq);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet post = new HttpGet(url);
        JSONObject response = null;
        try {
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(res.getEntity());// 返回json格式：
                response = JSONObject.fromObject(result);
                JSONArray jsonArray = response.getJSONObject("activity").getJSONArray("actions");
                if (!jsonArray.isEmpty()) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject obj = jsonArray.getJSONObject(i);
                        // 处理每个对象
                        EosModel eosModel = new EosModel();
                        eosModel.setFirst((Integer) response.getJSONObject("activity").get("first"));
                        eosModel.setLast((Integer) response.getJSONObject("activity").get("last"));
                        eosModel.setGlobal_action_seq(obj.get("global_action_seq").toString());
                        eosModel.setAccount_action_seq(obj.get("account_action_seq").toString());
                        eosModel.setBlock_num(obj.get("block_num").toString());
                        eosModel.setBlock_time(convertToDate(obj.get("block_time").toString()));
                        JSONObject act = obj.getJSONObject("action_trace").getJSONObject("act");

                        eosModel.setAccount(act.get("account").toString());
                        eosModel.setName(act.get("name").toString());
                        eosModel.setHex_data(act.get("hex_data").toString());

                        JSONObject data = obj.getJSONObject("action_trace").getJSONObject("act").getJSONObject("data");
                        eosModel.setFrom(data.get("from").toString());
                        eosModel.setTo(data.get("to").toString());
                        eosModel.setMemo(data.get("memo").toString());
                        String s = data.get("quantity").toString().split(" ")[0];
                        BigDecimal bigDecimal = new BigDecimal(s);
                        eosModel.setQuantity(bigDecimal);
                        eosModelList.add(eosModel);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return eosModelList;
    }

    public static Date convertToDate(String isoDateTime) throws ParseException, java.text.ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // 设置时区为UTC
        return sdf.parse(isoDateTime);
    }

    public static BigDecimal convert(String numberStr) {
        return new BigDecimal(numberStr);
    }
}